/**
 * 订单
 * Aaron create by 2019/06/14
 */
import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  getConnection,
  Double,
  ColumnType,
  OneToMany,
  ManyToOne,
  Index,
  Unique,
  OneToOne,
  AfterLoad,
} from 'typeorm';
import moment = require('moment');
import { User } from './user.entity';
import { Goods } from './goods.entity';

@Entity()
export class Orders {
  @PrimaryGeneratedColumn({
    comment: '自增ID',
  })
  id: number;

  @Column({
    nullable: false,
    comment: '订单号',
    length: 40,
  })
  orderCode: string;

  @ManyToOne((type) => User, (ClientUser) => ClientUser.orders)
  user: User;

  @ManyToOne((type) => Goods, (Goods) => Goods.orders)
  goods: Goods;

  @Column({
    type: 'int',
    nullable: false,
    comment: '用户id',
  })
  userId: number;

  @Column({
    type: 'int',
    nullable: false,
    comment: '商品id',
  })
  goodsId: number;

  @Column({
    nullable: true,
    comment: '用户头像',
    length: 200,
  })
  head_thumb: string;

  @Column({
    nullable: true,
    comment: '用户姓名',
    width: 30,
  })
  userName: string;

  @Column({
    type: 'int',
    nullable: false,
    comment: '订单类型(0待付款,1待发货,2已完成)',
  })
  orderClass: number;

  @Column({
    type: 'decimal',
    nullable: false,
    comment: '订单金额',
    precision: 7,
    scale: 2,
  })
  money: number;

  @Column({
    type: 'int',
    nullable: false,
    comment: '订单数量',
  })
  number: number;

  @Column({
    nullable: true,
    comment: '规格',
  })
  specifications: string;

  @CreateDateColumn({
    type: 'timestamp',
    nullable: false,
    comment: '创建时间',
    update: false,
    transformer: {
      to: (value) => {
        return value;
      },
      from: (value) => {
        return moment(value).format('YYYY-MM-DD HH:mm:ss');
      },
    },
  })
  creatTime: Date;
}
